/*
 * Copyright (c) 2021. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package cn.edu.ntu.yyl.service.imlp;


import cn.edu.ntu.yyl.dao.MenuDao;
import cn.edu.ntu.yyl.pojo.Menu;
import cn.edu.ntu.yyl.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    MenuDao Dao;
    // 增
    public Integer InsertNewMenu(Menu menu){
        //初始化

        try {
            //getMapper

            //执行SQL
            menu.setId(getmaxid());
            Dao.InsertNewMenu(menu);

        } catch (Exception e) {
            return -1;
        }
        return 1;
    }
    // 删
    public Integer DeleteMenu(Menu menu){
        //初始化

        try {
            //getMapper

            //执行SQL
            Dao.DeleteMenu(menu);

        } catch (Exception e) {
            return -1;
        }
        return 1;
    }
    // 改
    public Integer UpdateMenu(Menu menu){
        //初始化

        try {
            //getMapper

            //执行SQL
            Dao.UpdateMenu(menu);

        } catch (Exception e) {
            return -1;
        }
        return 1;
    }
    // 查
    public List<Menu> SelectAllMenu(){
        //初始化

        try {
            //getMapper

            //执行SQL
            return Dao.SelectAllMenu();
        } catch (Exception e) {
            return null;
        }
    }
    //更改菜单名字
    public Integer UpdateMenuText(Menu menu){
        //初始化

        try {
            //getMapper

            //执行SQL
            Dao.UpdateMenuText(menu);

        } catch (Exception e) {
            return -1;
        }
        return 1;
    }
    //更改菜单URL
    public Integer UpdateMenuURL(Menu menu){
        //初始化

        try {
            //getMapper

            //执行SQL
            Dao.UpdateMenuURL(menu);

        } catch (Exception e) {
            return -1;
        }
        return 1;
    }
    //获取最大ID
    private Integer getmaxid() {
        //初始化
        List<Menu> menus = null;
        Integer id = -1;
        //取得SqlSession对象(openSession)

        try {
            //getMapper

            //获取列表
            menus = Dao.SelectAllMenu();
        } catch (Exception e) {
            System.err.println(e.toString());

        }
        //获取最大ID然后+1便是需要写入的ID
        if (menus != null) {
            for (Menu menu : menus) {
                if (id < menu.getId()) {
                    id = menu.getId();
                }
            }
        } else return -1;
        id++;
        return id;
    }
}
